<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>复制一个闭包，绑定指定的$this对象和类作用域。</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="closure.construct.html">Closure::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="closure.bindto.html">Closure::bindTo</a></div>
 <div class="up"><a href="class.closure.html">Closure</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="closure.bind" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">Closure::bind</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.4.0)</p><p class="refpurpose"><span class="refname">Closure::bind</span> &mdash; <span class="dc-title">
   复制一个闭包，绑定指定的$this对象和类作用域。
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-closure.bind-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type"><a href="class.closure.html" class="type Closure">Closure</a></span> <span class="methodname"><strong>Closure::bind</strong></span>
    ( <span class="methodparam"><span class="type"><a href="class.closure.html" class="type Closure">Closure</a></span> <code class="parameter">$closure</code></span>
   , <span class="methodparam"><span class="type">object</span> <code class="parameter">$newthis</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$newscope</code>
   <span class="initializer"> = &#039;static&#039;</span></span>
  ] )</div>

  <p class="para rdfs-comment">
   这个方法是 <span class="methodname"><a href="closure.bindto.html" class="methodname">Closure::bindTo()</a></span> 的静态版本。查看它的文档获取更多信息。
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-closure.bind-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<em><code class="parameter">closure</code></em></dt>

    <dd>

     <p class="para">
      需要绑定的匿名函数。
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">newthis</code></em></dt>

    <dd>

     <p class="para">
      需要绑定到匿名函数的对象，或者
      <strong><code>NULL</code></strong> 创建未绑定的闭包。
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">newscope</code></em></dt>

    <dd>

     <p class="para">
      想要绑定给闭包的类作用域，或者 &#039;static&#039; 表示不改变。如果传入一个对象，则使用这个对象的类型名。
      类作用域用来决定在闭包中 $this 对象的 私有、保护方法 的可见性。
      The class scope to which associate the closure is to be associated, or
      &#039;static&#039; to keep the current one. If an object is given, the type of the
      object will be used instead. This determines the visibility of protected
      and private methods of the bound object.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-closure.bind-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个新的 <a href="class.closure.html" class="classname">Closure</a> 对象  或者在失败时返回 <strong><code>FALSE</code></strong>
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-closure.bind-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-303">
   <p><strong>Example #1 <span class="function"><strong>Closure::bind()</strong></span> 实例</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;static&nbsp;</span><span style="color: #0000BB">$sfoo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$ifoo&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">$cl1&nbsp;</span><span style="color: #007700">=&nbsp;static&nbsp;function()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">$sfoo</span><span style="color: #007700">;<br />};<br /></span><span style="color: #0000BB">$cl2&nbsp;</span><span style="color: #007700">=&nbsp;function()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">ifoo</span><span style="color: #007700">;<br />};<br /><br /></span><span style="color: #0000BB">$bcl1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Closure</span><span style="color: #007700">::</span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$cl1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'A'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$bcl2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Closure</span><span style="color: #007700">::</span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #0000BB">$cl2</span><span style="color: #007700">,&nbsp;new&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">'A'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$bcl1</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$bcl2</span><span style="color: #007700">(),&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
1
2
</pre></div>
   </div>
  </div>
 </div>



 <div class="refsect1 seealso" id="refsect1-closure.bind-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><a href="functions.anonymous.html" class="link">匿名函数</a></li>
   <li class="member"><span class="methodname"><a href="closure.bindto.html" class="methodname" rel="rdfs-seeAlso">Closure::bindTo()</a> - 复制当前闭包对象，绑定指定的$this对象和类作用域。</span></li>
  </ul>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="closure.construct.html">Closure::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="closure.bindto.html">Closure::bindTo</a></div>
 <div class="up"><a href="class.closure.html">Closure</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
